package com.wujie.hello.spring.cloud.alibaba.nacos.feign.consumer.controller;

import com.wujie.hello.spring.cloud.alibaba.nacos.feign.consumer.service.TestClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * @author wujie
 * @version :
 * @title TestController
 * @Description TODO
 * @dateTime 2020/9/7 9:40
 * @Copyright 2020-2021
 */
@RestController
@Slf4j
public class TestController {
    @Autowired
    TestClient testClient;
    @Value("${spring.application.name}")
    private String name;
    @RequestMapping("/hi")
    public String sayHi(){
        return testClient.sayHi(name);
    }

    /**
     * 不需要token访问测试
     * @return
     */
    @GetMapping("/test/no_need_token")
    public @ResponseBody
    String test() {
        return "no_need_token";
    }

    /**
     * 需要需要token访问接口测试
     * @return
     */
    @GetMapping("/test/need_token")
    public @ResponseBody String test2(Authentication authentication) {
        log.info("{}",authentication);
        // 由于自定义的principal返回的是包含全部user字段的map
        Object principal = authentication.getPrincipal();
        return "need_token";
    }

    /**
     * 需要需要管理员权限
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @GetMapping("/test/need_admin")
    public @ResponseBody String admin() {
        return "need_admin";
    }

}
